package com.ruoyi.mining.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mining.mapper.PaperMapper;
import com.ruoyi.mining.domain.Paper;
import com.ruoyi.mining.service.IPaperService;

/**
 * 试卷Service业务层处理
 *
 * @author mining
 * @date 2025-05-23
 */
@Service
public class PaperServiceImpl implements IPaperService {
    @Autowired
    private PaperMapper paperMapper;

    /**
     * 查询试卷
     *
     * @param id 试卷主键
     * @return 试卷
     */
    @Override
    public Paper selectPaperById(String id) {
        return paperMapper.selectPaperById(id);
    }

    /**
     * 查询试卷列表
     *
     * @param paper    试卷
     * @return 试卷
     */
    @Override
    public List<Paper> selectPaperList(Paper paper) {
        return paperMapper.selectPaperList(paper);
    }

    /**
     * 新增试卷
     *
     * @param paper 试卷
     * @return 结果
     */
    @Override
    public int insertPaper(Paper paper) {
        return paperMapper.insertPaper(paper);
    }

    /**
     * 修改试卷
     *
     * @param paper 试卷
     * @return 结果
     */
    @Override
    public int updatePaper(Paper paper) {
        paper.setCreateDate(DateUtils.getCurrentTime());
        int updateResult = 0;
        if (paper == null || StringUtils.isEmpty(paper.getId())) {//        数据库里面不存在 就插入

            paper.setId(MD5Utils.md5(paper.getTitle() + DateUtils.getCurrentTime()));

            updateResult = this.insertPaper(paper);
        } else {
            updateResult = paperMapper.updatePaper(paper);
        }

        return updateResult;
    }

    /**
     * 批量删除试卷
     *
     * @param ids 需要删除的试卷主键
     * @return 结果
     */
    @Override
    public int deletePaperByIds(String[] ids) {
        return paperMapper.deletePaperByIds(ids);
    }

    /**
     * 删除试卷信息
     *
     * @param id 试卷主键
     * @return 结果
     */
    @Override
    public int deletePaperById(String id) {
        return paperMapper.deletePaperById(id);
    }

    @Override
    public int downloadById(String id) {
        return paperMapper.downloadById(id);
    }
}
